package com.larry.mr.reducejoin;

import com.sun.org.apache.bcel.internal.generic.NEW;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.InputSplit;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;

import java.io.IOException;

/**
 * @ProjectName hadoop
 * @Author larry
 * @Date 15:37 2021/11/16
 * @Description TODO
 **/
public class ReduceJoinMapper extends Mapper<LongWritable, Text,Text,OrderPd> {

    private Text outK = new Text();
    private OrderPd outv = new OrderPd();
    private FileSplit inputSplit;

    @Override
    protected void setup(Context context) throws IOException, InterruptedException {
        inputSplit = (FileSplit) context.getInputSplit();
    }

    /**
     * 1001	01	1
     * 01	小米
     * @param key
     * @param value
     * @param context
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String line = value.toString();
        String[] split = line.split("\t");
        String name = inputSplit.getPath().getName();
        if (name.contains("order")){
            outK.set(split[1]);
            outv.setOrderId(split[0]);
            outv.setPid(split[1]);
            outv.setAmount(Integer.valueOf(split[2]));
            outv.setPname("");
            outv.setFlag("order");
        }else {
            outK.set(split[0]);
            outv.setOrderId("");
            outv.setPid(split[0]);
            outv.setAmount(0);
            outv.setPname(split[1]);
            outv.setFlag("pd");
        }
        context.write(outK,outv);
    }
}
